package org.fjsei.yewu.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import java.util.Arrays;

/**这个可能是 有状态模式 seesion MVC才走的路子？
 * 和CorsConfig有关联吗
 * */
@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**");
//            // .allowedOrigins("http://192.168.171.3:3765")
//        //后面运行org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.java:153

        registry.addMapping("/**")
//                .allowedOrigins("http://192.168.1.102:3765/")
                .allowCredentials(true)
                .allowedOriginPatterns("*")
                .allowedHeaders("*")
                .allowedMethods("*");
//                .maxAge(3600);
    }


    //【这个和TUS的上传没关系！】 为啥SecurityConfig.java也有配置！？ 这里针对 Actuator 端点的特定配置
    @Bean
    public CorsConfigurationSource actuatorCorsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOriginPatterns(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "OPTIONS"));
        configuration.setAllowedHeaders(Arrays.asList("*"));
        configuration.setAllowCredentials(true);
        configuration.setExposedHeaders(Arrays.asList("Set-Cookie"));  // 暴露 Set-Cookie 头

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/actuator/**", configuration);
        return source;
    }
}


//文件tus之前是 .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTION");
